QPixmap* MyMainWindow::convertIplimageToQPixmap( IplImage *newImage, bool transparency) {
	uchar *localQimageBuffer = NULL;
	int widthStep = newImage->widthStep;
	int height = newImage->height; 
	int channels = newImage->nChannels;
	int width = newImage->width;
	uchar *iplImagePtr = (uchar *) newImage->imageData;
	localQimageBuffer = (uchar *) calloc(widthStep*height,sizeof(uchar));
	memcpy(localQimageBuffer,iplImagePtr,widthStep*height*sizeof(uchar));
	uchar temp;
	if(channels!=1) { // swap RGB to BGR in same buffer
			int ptr = 0;
			for(int y=0; y<height; y++) {
				for(int p=0; p<width*channels; p+=channels) {
					ptr = y*widthStep;
					temp = localQimageBuffer[ptr+p];
					localQimageBuffer[ptr+p] = localQimageBuffer[ptr+p+2];
					localQimageBuffer[ptr+p+2] = temp;
				}
			}
	}

	QImage *tempImage;
	switch(channels) {
		case 1:
			tempImage = new QImage(localQimageBuffer, width, height,QImage::Format_Indexed8);
			break;
		case 3:
		default:
			tempImage = new QImage(localQimageBuffer, width, height,QImage::Format_RGB888);
			break;
	}
	if(resultPixmap!=NULL) {
		resultPixmap->~QPixmap();
		resultPixmap=NULL;
	}
	QPixmap resultPixmap = new QPixmap( QPixmap::fromImage(*tempImage));
	delete tempImage;
	free(localQimageBuffer);
	return &resultPixmap;
}
